package com.yl.zqy.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * @Description 接口返回数据格式
 * @Author liang.yu
 * @Date 2024/3/24 11:42
 */
@Data
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 成功标志
     */
    private boolean success = true;

    /**
     * 返回处理消息
     */
    private String message = "";

    /**
     * 返回代码
     */
    private Integer code = 0;

    /**
     * 返回数据对象 data
     */
    private T result;

    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();
    @JsonIgnore
    private String onlTable;

    public Result() {
    }

    /**
     * 兼容VUE3版token失效不跳转登录页面
     */
    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public static <T> Result<T> ok() {
        Result<T> r = new Result<>();
        r.setSuccess(true);
        r.setCode(HttpStatus.OK.value());
        return r;
    }

    public static Result<String> ok(String msg) {
        Result<String> r = new Result<>();
        r.setSuccess(true);
        r.setCode(HttpStatus.OK.value());
        //Result OK(String msg)方法会造成兼容性问题 issues/I4IP3D
        r.setMessage(msg);
        r.setResult(msg);
        return r;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> r = new Result<>();
        r.setSuccess(true);
        r.setCode(HttpStatus.OK.value());
        r.setResult(data);
        return r;
    }

    public static <T> Result<T> ok(T data, String message) {
        Result<T> r = new Result<>();
        r.setSuccess(true);
        r.setCode(HttpStatus.OK.value());
        r.setResult(data);
        r.setMessage(message);
        return r;
    }

    public static <T> Result<T> error(T data, String msg) {
        Result<T> r = new Result<>();
        r.setSuccess(false);
        r.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        r.setMessage(msg);
        r.setResult(data);
        return r;
    }

    public static <T> Result<T> error(T data, Integer code, String msg) {
        Result<T> r = new Result<>();
        r.setSuccess(false);
        r.setCode(code);
        r.setMessage(msg);
        r.setResult(data);
        return r;
    }

    public static <T> Result<T> error(String msg) {
        return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
    }

    public static <T> Result<T> error(int code, String msg) {
        Result<T> r = new Result<>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    public static <T> Result<T> error400(String message) {
        Result<T> r = new Result<>();
        r.setCode(HttpStatus.BAD_REQUEST.value());
        r.setMessage(message);
        r.setSuccess(false);
        return r;
    }

    /**
     * 未认证返回结果
     */
    public static <T> Result<T> unauthorized(String msg) {
        return error(HttpStatus.UNAUTHORIZED.value(), msg);
    }

    /**
     * 无权限访问返回结果
     */
    public static <T> Result<T> forbidden(String msg) {
        return error(HttpStatus.FORBIDDEN.value(), msg);
    }

}
